package cn.ll.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsDateJsonValueProcessor;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.JSONUtils;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import cn.ll.util.JsonDateValueProcessor;


public class Action extends MultiActionController {

	public void rtn(String rs, HttpServletRequest request,
			HttpServletResponse response) {

		response.setContentType("text/xml; charset=" + "UTF-8");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}

		writer.write(rs);
		writer.close();
	}

	public void rtn(List rs, HttpServletRequest request,
			HttpServletResponse response) {
		setDataFormat2JAVA();
		JSONArray array = JSONArray.fromObject(rs);
		this.rtn(array.toString(), request, response);

	}

	public void rtn(Object suc, HttpServletRequest request,
			HttpServletResponse response) {
		setDataFormat2JAVA();
		String jsonString = null;   
        //日期值处理器   
        JsonConfig jsonConfig = new JsonConfig();   
        jsonConfig.registerJsonValueProcessor(java.util.Date.class,
        		new JsonDateValueProcessor());
        
        
		JSONObject object = JSONObject.fromObject(suc,jsonConfig);
		this.rtn(object.toString(), request, response);

	}
	
	private static void setDataFormat2JAVA(){   
        //设定日期转换格式   
        JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"}));   
    }   

}
